package com.chinasofti.manager.controller;

import com.chinasofti.manager.domain.Student;
import com.chinasofti.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {

    StudentService  StudentService  = new StudentService();

    Scanner sc  = new Scanner(System.in);
    //开启学生管理系统
    public void start() {
        studentLoop:
        while (true){
            System.out.println("------------------------欢迎来到学生管理系统-----------------------");
            System.out.println("请输入你的选择：1.添加学生 2.删除学生 3.修改学生 4.查看学生 5.退出");
            String choice = sc.next();
            switch (choice){
                case "1" :
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2" :
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3" :
                    //System.out.println("修改学生");
                    updateStudentById();
                    break;
                case "4" :
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5" :
                    System.out.println("感谢使用学生管理系统，再见!");
                    break studentLoop;
                default:
                    System.out.println("你输入的有误，请重新输入：");
                    break;
            }
        }
    }

    private void updateStudentById() {
        //输入合法的ID
        String updateId = inputStudentId();
        //手动录入更新对象
        Student newStu = inputStudentInfo(updateId);
        //进行修改
        StudentService.updateStudent(updateId,newStu);
    }


    private void deleteStudentById() {

        String delId = inputStudentId();
        StudentService.deleteStudentById(delId);
        System.out.println("删除成功!");
    }

    private String inputStudentId() {

        String id;
        while (true){
            System.out.println("请输入学生id");
            id = sc.next();
            boolean exists = StudentService.isExists(id);
            if (exists){
                System.out.println("你输入的id不存在，请重新输入！");
            }else{
                break;
            }
        }
        return id;
    }

    public void findAllStudent(){
        Student[] stus = StudentService.findAllStudent();
        //判断stus是否为空
        if (stus == null){
            System.out.println("查无信息，请添加后重试！");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for(int i = 0;i<stus.length;i++){
            Student stu = stus[i];
            if (stu !=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBirthday());
            }
        }
    }



    public  void  addStudent(){
        String id;
        while (true){
            System.out.println("请输入学生的id");
            id = sc.next() ;
            boolean flag = StudentService.isExists(id);
            if (flag){
                System.out.println("学号可以使用！");
                break;
            }else {
                System.out.println("学号被占用，请重新输入！");
            }
        }
        //封装学生对象
        Student stu = inputStudentInfo(id);
        //开始添加学生到数组
        boolean result = StudentService.addStudent(stu);
        if (result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    private Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        String age = sc.next();
        System.out.println("请输入学生生日：（格式：年-月-日）");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);

        return stu;
    }
}
